package com.aries.ui.widget.demo.util;

import ohos.agp.components.*;

/**
 * Created: AriesHoo on 2017-01-17 12:59
 * Function: 视图帮助类
 * Desc:
 */
public class ViewUtil {
    private static volatile ViewUtil instance;

    private ViewUtil() {
    }

    public static ViewUtil getInstance() {
        if (instance == null) {
            synchronized (ViewUtil.class) {
                if (instance == null) {
                    instance = new ViewUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 获取视图的宽度
     *
     * @param mView
     * @return  视图的宽度
     */
    public int getWidth(Component mView) {
        if (mView == null) {
            return 0;
        }
        int w = Component.EstimateSpec.getSizeWithMode(0, Component.EstimateSpec.NOT_EXCEED);
        int h = Component.EstimateSpec.getSizeWithMode(0, Component.EstimateSpec.NOT_EXCEED);
        mView.estimateSize(w, h);
        int width = mView.getEstimatedWidth();
        return width;
    }

    /**
     * 获取视图高度
     *
     * @param mView
     * @return 视图高度
     */
    public int getHeight(Component mView) {
        if (mView == null) {
            return 0;
        }
        int w = Component.EstimateSpec.getSizeWithMode(0, Component.EstimateSpec.NOT_EXCEED);
        int h = Component.EstimateSpec.getSizeWithMode(0, Component.EstimateSpec.NOT_EXCEED);
        mView.estimateSize(w, h);
        int height = mView.getEstimatedHeight();
        return height;
    }

    /**
     * 设置视图的宽高
     *
     * @param view
     * @param width
     * @param height
     */
    public void setViewWidthAndHeight(Component view, int width, int height) {
        if (view == null) {
            return;
        }
        ComponentContainer.LayoutConfig lp = view.getLayoutConfig();
        lp.width = width;
        lp.height = height;
        view.setLayoutConfig(lp);
    }

    public void setViewWidth(Component view, int width) {
        if (view == null) {
            return;
        }
        ComponentContainer.LayoutConfig lp = view.getLayoutConfig();
        lp.width = width;
        view.setLayoutConfig(lp);
    }

    public void setViewHeight(Component view, int height) {
        if (view == null) {
            return;
        }
        ComponentContainer.LayoutConfig lp = view.getLayoutConfig();
        lp.height = height;
        view.setLayoutConfig(lp);
    }

    /**
     * 设置View的Margin
     *
     * @param view
     * @param left
     * @param top
     * @param right
     * @param bottom
     * @param width
     * @param height
     */
    public void setViewMargin(Component view, int left, int top, int right, int bottom, int width, int height) {
        if (view == null) {
            return;
        }
        ComponentContainer parent = (ComponentContainer) view.getComponentParent();
        if (parent == null) {
            return;
        }
        ComponentContainer.LayoutConfig lp;
        if (parent instanceof DirectionalLayout) {
            lp = new DirectionalLayout.LayoutConfig(width, height);
        } else if (parent instanceof DependentLayout) {
            lp = new DependentLayout.LayoutConfig(width, height);
        } else if (parent instanceof StackLayout) {
            lp = new StackLayout.LayoutConfig(width, height);
        } else {
            lp = new TableLayout.LayoutConfig(width, height);
        }
        lp.setMargins(left, top, right, bottom);
        view.setLayoutConfig(lp);
    }

    public void setViewMargin(Component view, int left, int top, int right, int bottom) {
        if (view == null) {
            return;
        }
        setViewMargin(view, left, top, right, bottom, getWidth(view), getHeight(view));
    }
}
